package controller;


import gui.catalogues.persons.PersonDTO;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import model.CollateralClient;
import model.DatabaseCommons;
import model.Person;
import model.PersonDAO;

public class PersonController {
	// DummyDB db = new DummyDB();
	PersonDAO db = new PersonDAO();
	DatabaseCommons dbc = new DatabaseCommons();

	public List<Person> getPeopleList() {
		return db.getElements();
	}

	public void addPerson(PersonDTO e) {
		/*
		 * tp.tpAppend("name = " + e.getName() + ", surname = " + e.getSurname()
		 * + ", isIndirectClient? " + e.isChkIndirectClient() +
		 * ", ItCollateralClient id = " + e.getItCollateralClient().getId() +
		 * ", ItCollateralClient desc = " + e.getItCollateralClient() + "\n");
		 */

		CollateralClient cc;
		switch (e.getItCollateralClient().getId()) {
		case 0:
			cc = CollateralClient.noCollateral;
			break;
		case 1:
			cc = CollateralClient.alejandroAlcaide;
			break;
		case 2:
			cc = CollateralClient.juanPerez;
			break;
		case 3:
			cc = CollateralClient.pedroPerez;
			break;
		default:
			cc = CollateralClient.noCollateral;
			;
		}

		/*
		 * 
		 * */
		Person p = new Person(e.getName(), e.getSurname(), cc,
				e.isChkIndirectClient());

		db.addPerson(p);
	}

	public void remPerson(int index) {
		db.remPerson(index);
	}

	public void savePeopleToFile(File file) throws IOException {
		db.saveToFile(file);
	}

	public void loadPeopleFromFile(File file) throws IOException,
			ClassNotFoundException {
		db.loadFromFile(file);
	}
	
	public void savePeopleToDB() throws SQLException {
		db.saveToDB();
	}
	
	public int getPeoplesMaxId() throws SQLException {
		return db.getMaxId();
	}

	public void loadPeopleFromDB() throws SQLException {
		db.loadFromDB();
	}
	
	public void addEmptyPerson() {
		System.out.println("Insert command - addEmptyPerson.");
		try {
			db.addEmptyPerson();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
